import { defineConfig } from "rollup";
import json from "@rollup/plugin-json";
import terser from "@rollup/plugin-terser";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { babel } from "@rollup/plugin-babel";
import { DEFAULT_EXTENSIONS } from '@babel/core';

import alias from "@rollup/plugin-alias";
import path, { dirname } from "path";
import { fileURLToPath } from "url";

import postcss from "rollup-plugin-postcss";
// import autoprefixer from "autoprefixer";
import cssnano from "cssnano";

import typescript from "rollup-plugin-typescript2";

import del from 'rollup-plugin-delete';
import { rimrafSync } from "rimraf";

import strip from "@rollup/plugin-strip";

import pkg from "./package.json" assert { type: "json" }; //断言导出json模块

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

rimrafSync("dist");// 删除打包目录

export default defineConfig([
  {
    input: "src/index.js", //入口文件
    output: [
        {
          dir: pkg.main, //出口文件夹
          format: "cjs", //打包成CommonJS模块
        //   manualChunks: {
        //     lodash: ["lodash-es"],
        //   },
        },
        {
          dir: pkg.module, //出口文件夹
          format: "es", //打包成es module模块
        //   manualChunks: {
        //     lodash: ["lodash-es"],
        //   },
        },
    //   {
    //     file: pkg.main, //出口文件
    //     format: "cjs", //打包成CommonJS模块
    //   },
    //   {
    //     file: pkg.module, //出口文件
    //     format: "es", //打包成es module模块
    //   },
    //   {
    //     name: "myUtils", //打包成UMD模式，需提供name
    //     file: pkg.browser, //出口文件
    //     format: "umd", //打包成UMD模块
    //   },
    //   {
    //       file: 'dist/bundle.min.js',
    //       format: 'iife',
    //       name: 'version',
    //       plugins: [terser()]
    //   }
    ],
    plugins: [
        json(),
        terser(),
        resolve(),
        commonjs(),
        alias({
            entries: [{ find: "@", replacement: path.resolve(__dirname, "src") }],
        }),

        // babel({
        //     babelHelpers: "runtime",
        //     presets: ["@babel/preset-env"],
        //     plugins: [["@babel/plugin-transform-runtime", { useESModules: true }]],
        // }),
        babel({
          babelHelpers: 'runtime',
          presets: ["@babel/preset-env"],
          plugins: [["@babel/plugin-transform-runtime", { useESModules: true }]],
          extensions: [...DEFAULT_EXTENSIONS, ".ts", ".tsx"], //增加配置
        }),
        // postcss(),
        postcss({
            plugins: [
                // autoprefixer(),
                cssnano()
            ],
            extract: "css/index.css",
        }),
        typescript(),
        // del({ targets: "dist/*" }),
        strip(),
    ],
  },
]);
